/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include <stdio.h>
#include "crc_func_def.h"

uint8 Reflect8(uint8 val) {
    uint8 resByte = 0;
    for(int i = 0; i < 8; i++)
    {
        if ((val & (1 << i)) != 0)
        {
            resByte |= ( (1 << (7 - i)) & 0xFF);
        }
    }
    return resByte;
}
uint16 Reflect16(uint16 val) {
    uint16 resByte = 0;
    for(int i = 0; i < 16; i++)
    {
        if ((val & (1 << i)) != 0)
        {
            resByte |= ( (1 << (15 - i)));
        }
    }
    return resByte;
}


uint32 Reflect32(uint32 val)
{
    uint32 resByte = 0;
    for(int i = 0; i < 32; i++)
    {
        if ((val & (1 << i)) != 0)
        {
            resByte |= ( (1 << (31 - i)));
        }
    }
    return resByte;
}




#define CRC_POLYNOMIAL_8    0x0C
uint8 crc_8(uint8 crc, uint8* pdata, uint32 len)
{
    for (uint32 i = 0; i < len; i++)
    {
        crc ^= pdata[i];
        for (uint8 j = 0; j < 8; j++)
        {
            if ((crc & 0x80u) > 0)
            {
                crc = ( (uint8)(crc << 1u) ) ^ CRC_POLYNOMIAL_8;
            }
            else
            {
                crc <<= 1u;
            }
        }
    }
    return crc;
}







// DEF_CRC_FUNC(gen_crc8, 8, crc8__table);
// DEF_CRC_FUNC(gen_crc16_maxim, 16, crc16_maxim__table);
// DEF_CRC_FUNC(gen_crc16_a, 16, crc16_arc__table);
// DEF_CRC_FUNC(gen_crc32_jamcrc, 32, crc32_jamcrc__table);


// int main(void)
// {
//     const uint8 buf[10] = "1234567890";
//     uint8 crc8 = gen_crc8(buf, 10, 0x00, 0x00, 0, 0);
//     uint16 crc16_maxim = gen_crc16_maxim(buf, 10, 0x0000, 0xFFFF, 1, 1);
//     uint16 crc16_a = gen_crc16_a(buf, 10, 0xC6C6, 0x0000, 1, 1);
//     uint32 crc32_jamcrc = gen_crc32_jamcrc(buf, 10, 0xFFFFFFFF, 0xFFFFFFFF, 1, 1);
//     printf("crc8: %X\n", crc8);
//     printf("crc16_maxim: %X\n", crc16_maxim);
//     printf("crc16_a: %X\n", crc16_a);
//     printf("crc32_jamcrc: %X\n", crc32_jamcrc);
// }
